const { Sequelize } = require("sequelize");
const { isRequired } = require("../utils");

module.exports = (sequelize, DataTypes) => {
  const DictItem = sequelize.define(
    "DictItem",
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: Sequelize.UUIDV4,
        //是否允许为空
        allowNull: false,
        //是否为主键
        primaryKey: true,
        //备注
        comment: "字典组id",
      },
      dictId: {
        type: DataTypes.UUID,
        allowNull: false,
        comment: "字典组id",
        validate: isRequired("字典组id"),
      },
      label: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "字典项名称",
        validate: isRequired("字典项名称"),
      },
      value: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "字典项值",
        validate: isRequired("字典项值"),
      },
      status: {
        type: DataTypes.ENUM("0", "1"),
        allowNull: false,
        comment: "字典项启用状态: 0: 不启用 1: 启用",
        validate: {
          ...isRequired("启用状态"),
          isIn: {
            args: [["0", "1"]],
            msg: "status字段只能传0或者1",
          },
        },
      },
      remark: {
        type: DataTypes.STRING,
        comment: "字典组描述",
      },
    },
    {
      timestamps: true,
      tableName: "system_dict_item",
      createdAt: "createTime",
      updatedAt: "updateTime",
      comment: "字典项信息表",
    }
  );
  // DictItem.associate(
  DictItem.associate = function (db) {
    DictItem.belongsTo(db.Dict, {
      foreignKey: "dictId",
      as: "dict",
    });
  };

  return DictItem;
};
